package com.xiaotu.memcached.listener;

import cn.hutool.core.util.ObjectUtil;
import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * @author zhangxiao
 * @create 2022-04-08 10:23
 */
@Component
public class ApplicationClosedEventListener implements ApplicationListener<ContextClosedEvent> {

    @Autowired
    private MemcachedClient memcachedClient;

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        if (ObjectUtil.isNotNull(memcachedClient))
            memcachedClient.shutdown();
    }
}
